<?php
/**
 * 
 * @authors Gary_zhou
 * @date    2016-06-29 23:50:15
 * @version v1.0.0
 * 解析csv
 */

require ('./xml2array.php');

	/*

		上传保存csv文件
			提取文件内容
	*/

				    $csv_file = fopen('upload/share1467220133.csv', 'r');

				    $result_arr = input_csv($csv_file);
				    fclose($csv_file);

				    //$dataArray = array_merge($dataArray,$result_arr);
				    //var_dump($dataArray);
				    var_dump($data);
				    set_js($dataArray,$result_arr);
	//getFile();
    function getFile(){
    	//var_dump($_FILES["share"]);
        if (($_FILES["share"]["size"] < 100000)){

			  if ($_FILES["share"]["error"] > 0)
			    {
			    echo "Return Code: " . $_FILES["share"]["error"] . "<br />";
			    }
			  else
			    {

				    $str_name = explode(".",$_FILES["share"]["name"]);
				    $n = $str_name[0].strtotime(date('Y-m-d H:i:s')).'.'.$str_name[1];

				    move_uploaded_file($_FILES["share"]["tmp_name"],"./upload/" . $n);
				    $csv_filename = $n;
				    //$csv_file = fopen('upload/'.$csv_filename, 'r');
				    $csv_file = fopen('upload/share1467220133.csv', 'r');

				    $result_arr = input_csv($csv_file);
				    fclose($csv_file);
				    var_dump($data);
				    var_dump($dataArray);

				    echo json_encode(array('code' => 0));
				    //$this->set_js($result_arr);
			    }
		}else{
			echo "Invalid file";
		}
    }
    function input_csv($csv_file) {
	    $result_arr = array ();
	    $i = 0;
	    $j = 0;

	    while ($data_line = fgetcsv($csv_file, 10000)) {
	    	$j = 0;
	        if($i == 0){
	            $GLOBALS['csv_key_name_arr'] = $data_line;
	            $i++;
	            continue;
	        }
	        foreach($GLOBALS['csv_key_name_arr'] as $csv_key_num=>$csv_key_name){
	            //$result_arr[$i-1][$csv_key_name] = $data_line[$csv_key_num];
	            $result_arr[$i-1][$j] = $data_line[$csv_key_num];

	            $j++;
	        }
	        $i++;
	    }

	    return $result_arr;
	}
    /*
		解析csv数据
    */
    function set_js($old,$new){

    	//$share = array_merge($old,$new);
    	$share = $new;

		$xml_start = '<?xml version="1.0" encoding="UTF-8"?>
						<shares>';

		$xml_end = '</shares>';

		$xml_tpl = "";

		foreach ($share as $k => $v) {
			$v[0] = isset($v[0])?$v[0]:'';
			$v[1] = isset($v[1])?$v[1]:'';
			$v[2] = isset($v[2])?$v[2]:'';
			$v[3] = isset($v[3])?$v[3]:'';
			$v[4] = isset($v[4])?$v[4]:'';

			$xml_tpl .= "
				<share id='".$k."'>
					<url>$v[0]</url>
					<title>$v[1]</title>
					<desc>$v[2]</desc>
					<shareimg>$v[3]</shareimg>
					<shareurl>$v[4]</shareurl>
				</share>
				";
		}

		$xml = $xml_start.$xml_tpl.$xml_end;

		$share_xml = fopen("../db/wxShare.xml", "r+") or die("Unable to open file!");

		fwrite($share_xml, $xml);

		fclose($share_xml);

		share_js($share);
	}
	/*
		导出js
	*/
	function share_js($share){  

		foreach ($share as $k => $v) {
			$share[$k][0] = isset($v[0])?'"url"\|'.'"'.$v[0].'"':'"url"\|'.'""';
			$share[$k][1] = isset($v[1])?'"title"\|'.'"'.$v[1].'"':'"title"\|'.'""';
			$share[$k][2] = isset($v[2])?'"desc"\|'.'"'.$v[2].'"':'"desc"\|'.'""';
			$share[$k][3] = isset($v[3])?'"shareimg"\|'.'"'.$v[3].'"':'"shareimg"\|'.'""';
			$share[$k][4] = isset($v[4])?'"shareurl"\|'.'"'.$v[4].'"':'"shareurl"\|'.'""';
		}

	    $share_js = fopen("share.js", "w") or die("Unable to open file!");
		$share_start = "var xmWxShare = { \n";

		$share_body_start = "";
		$share_body_body = "";
		$share_body_end = "";

		$share_body = "";

		foreach ($share as $k2 => $v2) {

			$share_body_start = explode('\|',$v2[0])[1]." : { \n";
			$share_body_body = "";

			foreach ($v2 as $k3 => $v3) {
				$share_body_body .= explode('\|',$v3)[0].":".explode('\|',$v3)[1].",\n";
			}
			$share_body_end = "},\n";

			$share_body .= $share_body_start.$share_body_body.$share_body_end;
		}

		$share_end = "}\n";


		$share = $share_start.$share_body.$share_end;

		fwrite($share_js, $share);
		fclose($share_js);

		echo json_encode(array('code' => 0));

	}